package ch.usi.inf.pf2;

import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;

import javax.swing.JComponent;

public class MegaOutputDrawer extends JComponent implements java.io.Serializable{
	
	private PanicSimDrawingArea area;
	private int oldX = 20;
	private int oldY = 25;
	private int actual;
	private int n;
	private ArrayList<Integer> drawingValue = new ArrayList<Integer>();
	
	public MegaOutputDrawer(PanicSimDrawingArea area){
		this.area = area;
		new Thread(new Runnable() {
			public void run() {repaint();}}).start();
	}

	public void setPos(int n) {
		this.n = n;
	}
	
	public void paintComponent(final Graphics g) {

		g.setColor(Color.WHITE);
		g.fillRect(5, 5, this.getParent().getWidth() - 5, this.getParent()
				.getHeight() - 5);

		g.setColor(Color.BLACK);
		
for(int i = 0; i < drawingValue.size(); i=i+4){
			
			g.drawLine(drawingValue.get(i), drawingValue.get(i+1), drawingValue.get(i+2), drawingValue.get(i+3));
			
		}
		

		if (area.getSelectedMegaOutput() != null) {

			if (area.getSelectedMegaOutput().getValue(n) == null) {
				drawingValue.add(oldX);
				drawingValue.add(25);
				drawingValue.add(oldX+10);
				drawingValue.add(25);
				
				oldX=oldX+10;
			}

			else if (area.getSelectedMegaOutput().getValue(n) == 1
					&& area.getSelectedMegaOutput().getValue(n) != actual) {
			
				drawingValue.add(oldX);
				drawingValue.add(15);
				drawingValue.add(oldX);
				drawingValue.add(25);
				
				oldY = 15;
				
				drawingValue.add(oldX);
				drawingValue.add(oldY);
				drawingValue.add(oldX+10);
				drawingValue.add(oldY);
				
				actual = 1;
				
				oldX = oldX+10;
			}

			else if (area.getSelectedMegaOutput().getValue(n) == 0
					&& area.getSelectedMegaOutput().getValue(n) != actual) {
			
				drawingValue.add(oldX);
				drawingValue.add(15);
				drawingValue.add(oldX);
				drawingValue.add(25);
				
				oldY = 25;
				
				drawingValue.add(oldX);
				drawingValue.add(oldY);
				drawingValue.add(oldX+10);
				drawingValue.add(oldY);
				
				actual = 0;
				oldX = oldX +10;
			}
			
			
			else if (area.getSelectedMegaOutput().getValue(n) == actual) {

				
				drawingValue.add(oldX);
				drawingValue.add(oldY);
				drawingValue.add(oldX+10);
				drawingValue.add(oldY);

				oldX = oldX +10;
			}
			
			if(oldX > 650){
				
				for(int i = 0; i < drawingValue.size(); i=i+4){
					
					drawingValue.set(i, drawingValue.get(i) -  10);
					drawingValue.set(i+2, drawingValue.get(i+2) - 10);}
				
			oldX = oldX - 10;}



			
		} else {
			oldX = 20;
			g.setColor(Color.WHITE);
			g.fillRect(5, 5, this.getParent().getWidth() - 5, this.getParent()
					.getHeight() - 5);
			
			drawingValue = new ArrayList<Integer>();
		}

		try {
			Thread.sleep(104);
		} catch (Exception e) {
		}
		
			
		}

	}

